﻿<?xml version="1.0" encoding="utf-8"?>
<!-- 
    Spun AJAX Framework
    Copyright (C) 2008 Blake Hegerle

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
-->
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:html="http://www.w3.org/1999/xhtml"
                xmlns:n="urn:ndope-org:view-v1"
                exclude-result-prefixes="html n">
    <xsl:import href="Views.xslt"/>

    <xsl:param name="content-base-URL" select="'Content'" />

    <xsl:output method="xml"
                indent="yes"
                version="1.0"
                encoding="utf-8"
                omit-xml-declaration="no"
                doctype-public="-//W3C//DTD XHTML 1.1//EN"
                doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"/>

    <xsl:template match="html:html">
        <html>
            <head>
                <script type="text/javascript" src="{concat($content-base-URL, '/prototype-1.6.0.2.js')}" />
                <script type="text/javascript" src="{concat($content-base-URL, '/Spun-1.0.0.js')}" />

                <xsl:apply-templates select="n:import" />

                <xsl:apply-templates select="html:head" />
            </head>

            <xsl:apply-templates select="html:body" />
        </html>
    </xsl:template>

    <xsl:template match="n:import">
        <script type="text/javascript" src="{concat('views/', @src, '.view')}" />
    </xsl:template>

    <xsl:template match="html:body">
        <body>
            <xsl:for-each select="@*">
                <xsl:copy />
            </xsl:for-each>

            <xsl:apply-templates select="*" />

            <script type="text/javascript">
                <xsl:text>&#10;</xsl:text>
                <xsl:apply-templates select="*" mode="init-construction" />

                <xsl:text>&#10;</xsl:text>

                <xsl:text>StartSpun();&#10;</xsl:text>
            </script>
        </body>
    </xsl:template>

    <xsl:template match="*">
        <xsl:element name="{local-name(.)}">
            <xsl:for-each select="@*">
                <xsl:copy />
            </xsl:for-each>

            <xsl:apply-templates select="child::node()" />
        </xsl:element>
    </xsl:template>

    <xsl:template match="*[@n:*]">
        <xsl:element name="{local-name(.)}">
            <xsl:for-each select="@*[namespace-uri() = '']">
                <xsl:if test="name(.) != 'id'">
                    <xsl:copy />
                </xsl:if>
            </xsl:for-each>

            <xsl:attribute name="id">
                <xsl:call-template name="get-id" />
            </xsl:attribute>
        </xsl:element>
    </xsl:template>

    <xsl:template name="get-id">
        <xsl:choose>
            <xsl:when test="@id">
                <xsl:value-of select="@id"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="generate-id(.)"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!-- Construction -->

    <xsl:template match="html:*" mode="init-construction">
        <xsl:apply-templates select="*" mode="init-construction" />
    </xsl:template>

    <xsl:template match="*[@n:*]" mode="init-construction">
        <xsl:apply-templates select="." mode="construction" />

        <xsl:text>.attach('</xsl:text>
        <xsl:call-template name="get-id" />
        <xsl:text>');&#10;</xsl:text>

        <!--xsl:if test="count(@n:Sequence) = 0">
            <xsl:apply-templates select="child::node()" mode="init-construction" />
        </xsl:if-->
    </xsl:template>
</xsl:stylesheet>
